# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=kicad
pkgbase=mingw-w64-${_realname}-git
_sub=("packages3D")
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-${_sub}-git")
pkgver=r1250.239c587e
pkgrel=1
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=("any")
url="http://www.kicad-pcb.org"
license=("GPLv3+")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "git")
source=("${_realname}-${_sub}"::"git+https://github.com/KiCad/kicad-${_sub}.git")
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}/${_realname}-${_sub}"
  ( set -o pipefail
#    git describe --long 2>/dev/null | sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
  )
}

prepare() {
  for _i in ${_sub[@]}; do
    [[ -d build-${_i} ]] && rm -r build-${_i}
  done
  true
}

build_sub() {
  cd "${srcdir}"
  mkdir -p build-${1} && cd build-${1}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${1}
}

for _s in ${_sub[@]}; do
  eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-${_s}-git() {
    options=('!strip')
    conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-${_s}")
    build_sub ${_s}
    cd "\"\${srcdir}\"/build-${_s}"
    make DESTDIR="\"\${pkgdir}\"" install
  }"
done
